<?php
session_start();
require_once("../config/include_tokyo.inc.php");
require_once("../class/Core_Lib_Page.php");
require_once("../class/Common_Function.php");
$commonFunction = new Common_Function();

//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 || $userRole == 7){
	//管理员 + 女装
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}

//任务操作
$doFlag = $_POST["doFlag"];
$searchKeyword = $_POST["search_keyword"];
$validateFlag = true;

if($doFlag == "addFinancial"){
	//新增记录
	$io_flag =  $_POST["io_flag"];
	$amount =  $_POST["amount"];
	$note =  $_POST["note"];
	$amount = str_replace(",","",$amount);
	$amount = trim($amount);
	if(eregi("^[0-9]+$",$amount)){
			//是数字
			if($amount < 0){ //转为正数
				$amount = $amount*-1;
			}
			if($io_flag == 1 || $io_flag == 2 || $io_flag == 3 || $io_flag == 11){
				//转入为正
			}else{
				//转出为负
				$amount = $amount*-1;
			}
	}else{
			$validateFlag = false;
			$insertError = $insertError."  金额只能是数字";
	}	
	if($validateFlag){
		$sql = "insert into financial(create_time,io_flag,amount,note) values (NOW(),$io_flag,$amount,'$note')";
		$db->Execute($sql);	
	}		
}else if($doFlag == "doDelete"){
	//删除记录
	$id =  $_POST["financialId"];
	$sql = "delete from financial where id=".$id;
	$db->Execute($sql);
}else if($doFlag == "insertAlipayAmount"){
	//更新支付宝当前余额 
	$current_amount =  $_POST["current_amount"];
	$current_amount = str_replace(",","",$current_amount);
	$current_amount = trim($current_amount);
	if(eregi("^[0-9]+$",$current_amount)){
			//是数字
			if($amount < 0){ //转为正数
				$amount = $amount*-1;
			}			
	}else{
			$validateFlag = false;
			$insertError = $insertError."  金额只能是数字";
	}	
	if($validateFlag){
		$sql = "insert into clothingalipay(create_time,current_amount,note) values (NOW(),$current_amount,'From Alipay')";
		$db->Execute($sql);	
	}	

}


//显示列表 & 分页设置
$listFlag = $_POST["listFlag"];
if($listFlag == ""){
	$listFlag = "1";
}
$list_rows = 50; //每页显示的记录数
$sql_all = "SELECT COUNT(*) as total_rows from financial"; // 统计总数

$result = $db->Execute($sql_all);
while (!$result->EOF) {
	$totalRows = $result->fields['total_rows'];
	$result->MoveNext();
}
$nowPage = $_POST['nowPage'];
$nowPage = !empty($nowPage) ? $nowPage : 1;
$params = array(
	'total_rows'=>$totalRows,
	'list_rows'=>$list_rows,
	'method'    =>'ajax',
	'ajax_func_name' =>'goToPage',
	'now_page' => $nowPage 
);
$page = new Core_Lib_Page($params);
$limit_m = $page->first_row;
$limit_n = $list_rows;

$sql = "SELECT * from financial f  order by f.create_time desc limit $limit_m,$limit_n"; //默认 全部
$result = $db->Execute($sql);

//**********支付宝余额***********
$sql_alipay = "SELECT * from clothingalipay order by create_time desc"; //默认 全部
$result_alipay = $db->Execute($sql_alipay);

/*********现金账户*********************/
$sql_cash = "SELECT sum(amount) as case_amount from financial";
$result_caseAmount = $db->Execute($sql_cash);

/*********总资金统计*********************/
$total_amount = $result_alipay->fields['current_amount'] + $result_caseAmount->fields['case_amount'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "财务管理"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../clothingMenu.php"); ?>


<form name="listForm" action="../clothing/financialList.php" method="post">
<div id="container" >	



<!----------------支付宝余额----------------------------->
<div class="grayTable">	
<table border=0.1 >
<thead>
	<tr>
		<th colspan=4>支付宝余额</th>
	</tr>
	<tr>
		<td>更新时间:<?php echo $result_alipay->fields['create_time'];?></td>
		<td>当前余额:<font  style="font-size:120%;color:red;"><?php echo $result_alipay->fields['current_amount'];?></font>(RMB) </td>
		<td><input type="text" name="current_amount" id="current_amount"  size=30> <input type="button" name="更新余额" value="更新余额" onclick="insertAlipayAmount()"></td>
	</tr>
</thead>					
</table>
</div>
<br>



<!----------------新增记录----------------------------->
<div class="grayTable">	
<table >
<thead>
	<tr>
		<th scope="col" >资金流向</th>
		<th scope="col" >金额</th>
		<th scope="col" >备注</th>
		<th scope="col" >操作</th>
	</tr>
	<tr>
		<td>
			<select name="io_flag" id="io_flag">
			<option value=1  ><?php echo $commonFunction->getIOFlagName(1);?></option>
			<option value=2  ><?php echo $commonFunction->getIOFlagName(2);?></option>
			<option value=3  ><?php echo $commonFunction->getIOFlagName(3);?></option>
			<option value=11  ><?php echo $commonFunction->getIOFlagName(11);?></option>
			<option value=4  ><?php echo $commonFunction->getIOFlagName(4);?></option>
			<option value=5  ><?php echo $commonFunction->getIOFlagName(5);?></option>
			<option value=6  ><?php echo $commonFunction->getIOFlagName(6);?></option>
			<option value=7  ><?php echo $commonFunction->getIOFlagName(7);?></option>
			<option value=8  ><?php echo $commonFunction->getIOFlagName(8);?></option>
			<option value=9  ><?php echo $commonFunction->getIOFlagName(9);?></option>
			<option value=10  ><?php echo $commonFunction->getIOFlagName(10);?></option>
			</select>
		</td>
		<td><input type="text" name="amount" id="amount"  size=8></td>
		<td><input type="text" name="note" id="note"  size=30></td>
		<td><input type="button" name="提交" value="提交" onclick="addFinancial()"></td>
	</tr>
</thead>					
</table>
</div>




<?php
if(!$validateFlag){
	echo "<font color='red'>新增记录出错: ".$insertError."</font>";
}
?>


<!----------------总资金统计----------------------------->
<br>
<b>资金总额：支付宝 (<?php echo $result_alipay->fields['current_amount'];?>) + 现金账户 (<?php echo $result_caseAmount->fields['case_amount'];?>) = <?php echo $total_amount; ?></b>
<div class="clearBoth">&nbsp;</div>



<div class="grayTable">	
<!----------------记录列表----------------------------->
<table >
<thead>
	<tr>
	    <th scope="col" >记录编号</th>
	    <th scope="col" >创建时间</th>
		<th scope="col" >资金流向</th>
	    <th scope="col" >金额</th>
		<th scope="col" >备注</th>
		<th scope="col" width=95>操作</th>
	</tr>
</thead>

<tbody>	
<?php
	if(empty($result) || $result->RecordCount() < 1){
		echo "<tr><td colspan=6 align='center'>记录为空.</td></tr>";
	}else{		
		while (!$result->EOF) {	
			$ioFlag = $result->fields['io_flag'];
			if($ioFlag == 1 || $ioFlag == 2 || $ioFlag == 3){
				//入账
				$trClass = "class='trRed'";
			}else{
				//出帐
				$trClass = "class='trGreen'";
			}			

			$strTR = "<tr ".$trClass.">";
			$strTR = $strTR."<td>".$result->fields['id']."</td>";
			$strTR = $strTR."<td>".$result->fields['create_time']."</td>";
			$strTR = $strTR."<td>".$commonFunction->getIOFlagName($ioFlag)."</td>";
			$strTR = $strTR."<td>".$result->fields['amount']."</td>";
			$strTR = $strTR."<td>".$result->fields['note']."</td>";
			$strTR = $strTR."<td>";
			$strTR = $strTR."<input type='button' value='编辑' onclick='doEdit(".$result->fields['id'].")'>";
			$strTR = $strTR."<input type='button' value='删除' onclick='doDelete(".$result->fields['id'].")'>";
			$strTR = $strTR."</td>";
			$strTR = $strTR."</tr>";
			echo $strTR;
			$result->MoveNext();	
		}
	}

?>
</tbody>
					
</table>	
<br>

<!--Page 分页 -->
<div id="page"><?php echo $page->show(2);?></div>

</div>



</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" value="<?php echo $doFlag; ?>">
<input type="hidden" name="listFlag" id="listFlag" value="<?php echo $listFlag; ?>">
<input type="hidden" name="financialId" id="financialId" value="<?php echo $financialId; ?>">
<input type="hidden" name="nowPage" id="nowPage" value="<?php echo $nowPage; ?>">
</form>
<br><br><br>
</body>




</html>

<script>
/************新增记录**************************/
function addFinancial(){
	var amount = $("#amount").val();	
	if(amount==""){
		alert("金额不能为空");
		$("#amount").focus();  
		return;
	}
	var note = $("#note").val();	
	if(note==""){
		alert("备注不能为空");
		$("#note").focus();  
		return;
	}
	document.getElementById('doFlag').value = "addFinancial";
	document.listForm.submit();
}


/************更新支付宝当前余额**************************/
function insertAlipayAmount(){
	var current_amount = $("#current_amount").val();	
	if(current_amount==""){
		alert("金额不能为空");
		$("#current_amount").focus();  
		return;
	}
	document.getElementById('doFlag').value = "insertAlipayAmount";
	document.listForm.submit();
}


/************删除记录**************************/
function doDelete(id){	
	if(confirm("确认要删除编号为: "+id+" 的记录?")){
		document.getElementById('doFlag').value = "doDelete";
		document.getElementById('financialId').value = id;
		document.listForm.submit();
	}	
}

/************编辑记录**************************/
function doEdit(id){	
	window.location.href="financialInput.php?id="+id; 
}

</script>

